package com.karry.yl.service.elder.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.impl.SuperServiceImpl;
import com.karry.yl.dao.elder.CheckInRecordsMapper;
import com.karry.yl.domain.baseinfo.Bed;
import com.karry.yl.domain.baseinfo.Room;
import com.karry.yl.domain.elder.CheckInRecords;
import com.karry.yl.domain.elder.Elderly;
import com.karry.yl.domain.services.Meal;
import com.karry.yl.service.baseinfo.BedService;
import com.karry.yl.service.baseinfo.RoomService;
import com.karry.yl.service.elder.CheckInRecordsService;
import com.karry.yl.service.elder.ElderlyService;
import com.karry.yl.service.personnel.DepartmentService;
import com.karry.yl.service.services.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class CheckInRecordsServiceImpl extends SuperServiceImpl<CheckInRecordsMapper,CheckInRecords> implements CheckInRecordsService {
    @Autowired
    private ElderlyService elderlyService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private BedService bedService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private MealService mealService;
    @Autowired
    private  CheckInRecordsService checkInRecordsService;
    @Override
    @Transactional
    public void save(CheckInRecords checkInRecords) {
        baseMapper.insert(checkInRecords);
        Elderly elderly = elderlyService.selectOne(new EntityWrapper<Elderly>().eq("id", checkInRecords.getElderlyId()).eq("orgId", checkInRecords.getOrgId()).eq("deleted",false));
        elderly.setCheckedIn(true);
        elderly.setEnterDate(new Date());
        elderly.setMealId(checkInRecords.getMealId());
        elderly.setNursingDepartmentId(checkInRecords.getNursingDepartmentId());
        if(checkInRecords.getNursingDepartmentId()!=null){
            elderly.setAllocationStart(true);
        }
        Meal meal = mealService.selectById(checkInRecords.getMealId());
        if(meal!=null){
            elderly.setMealName(meal.getName());
        }
        elderlyService.updateById(elderly);
        //更新入住床位的状态
        Bed bed = bedService.selectOne(new EntityWrapper<Bed>().eq("id", checkInRecords.getBedId()).eq("deleted", false));
        bed.setStatus(Bed.STATUS_YZ);
        bedService.updateById(bed);
        //检查某个房间是否满员
        Room room = roomService.selectById(bed.getRoomId());
        int count = bedService.countByRoomId(bed.getRoomId());
        if(count >= room.getBedNum()){
            room.setStatus(Room.STATUS_MY);
            roomService.updateById(room);
        }
    }

    @Override
    public Page<CheckInRecords> selectListPage(Page<CheckInRecords> page,Long orgId) {
        return  page.setRecords(baseMapper.selectListPage(page,orgId));
    }

    @Override
    public List<String> selectYizhuElderRooms(CheckInRecords checkInRecords) {
        return baseMapper.selectYizhuElderRooms(checkInRecords);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void checkInUpdate(CheckInRecords checkInRecords,Long orgId) {
        //根据老人id查询该老人的入住记录
        if(!StringUtils.isEmpty(checkInRecords.getBedId())){
            CheckInRecords oldCheckInRecords = checkInRecordsService.selectOne(new EntityWrapper<CheckInRecords>().eq("orgId", orgId).eq("elderlyId", checkInRecords.getElderlyId()));
            //获取老人现在入住的床位Id
            String bedId = oldCheckInRecords.getBedId();
            //根据老人的旧床位id查询床位
            Bed bed = bedService.selectById(bedId);
            //更新床位状态
            bed.setStatus(1);
            bedService.updateById(bed);
            //根据需要变更的床位Id查询出该床位
            Bed newBed = bedService.selectById(checkInRecords.getBedId());
            //跟新床位状态更改为，已经住人
            newBed.setStatus(2);
            bedService.updateById(newBed);
            //跟新入住信息
            checkInRecords.setId(oldCheckInRecords.getId());
            checkInRecordsService.updateById(checkInRecords);
        }
        //跟新老人的护理级别，护理部门
        Meal meal = mealService.selectById(checkInRecords.getMealId());
        Elderly elderly = elderlyService.selectById(checkInRecords.getElderlyId());
        elderly.setNursingDepartmentId(checkInRecords.getNursingDepartmentId());
        elderly.setMealId(meal.getId());
        elderly.setMealName(meal.getName());
        elderlyService.updateById(elderly);
    }

}
